package com.tengju.user.infrastructure.repository;

import com.google.common.collect.Lists;
import com.tengju.user.domain.model.user.DominationMemberNote;
import com.tengju.user.domain.model.user.DominationMemberNoteRepository;
import com.tengju.user.domain.model.user.UserIdCode;
import com.tengju.user.infrastructure.persistence.mybatis.dataobject.DominationMemberNoteDO;
import com.tengju.user.infrastructure.persistence.mybatis.mapper.dealer.custom.DominationMemberNoteMapper;
import com.tengju.user.infrastructure.repository.convert.DominationMemberNoteConvert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Repository
public class DominationMemberNoteRepositoryImpl implements DominationMemberNoteRepository {

    @Resource
    DominationMemberNoteMapper dominationMemberNoteMapper;

    @Override
    public Boolean edit(UserIdCode leaderId, UserIdCode subordinateId, String remark) {
        DominationMemberNoteDO dominationMemberNoteDO = new DominationMemberNoteDO();
        dominationMemberNoteDO.setLeaderId(leaderId.getValue());
        dominationMemberNoteDO.setSubordinateId(subordinateId.getValue());
        dominationMemberNoteDO.setRemark(remark);
        dominationMemberNoteDO.setCreateTime(LocalDateTime.now());
        dominationMemberNoteDO.setUpdateTime(LocalDateTime.now());
        dominationMemberNoteMapper.edit(dominationMemberNoteDO);
        return true;
    }

    @Override
    public Map<UserIdCode,List<DominationMemberNote>> getBySubordinateIds(List<UserIdCode> subordinateIds) {

        List<Long> subordinateIdValues = subordinateIds.stream().map(UserIdCode::getValue).collect(Collectors.toList());

        List<DominationMemberNoteDO> dominationMemberNoteDOList =
                dominationMemberNoteMapper.queryBySubordinateIds(subordinateIdValues);

        HashMap<UserIdCode, List<DominationMemberNote>> noteMap = new HashMap<>();

        if (CollectionUtils.isEmpty(dominationMemberNoteDOList)) {
            return noteMap;
        }

        for (DominationMemberNoteDO dominationMemberNoteDO:dominationMemberNoteDOList) {

            try {
                UserIdCode subordinateId = new UserIdCode(dominationMemberNoteDO.getSubordinateId());
                List<DominationMemberNote> dominationMemberNotes = noteMap.get(subordinateId);
                if (CollectionUtils.isEmpty(dominationMemberNotes)) {
                    List<DominationMemberNote> dominationMemberNoteList = Lists.newArrayList();
                    dominationMemberNoteList.add(DominationMemberNoteConvert.convert(dominationMemberNoteDO));
                    noteMap.put(subordinateId,dominationMemberNoteList);
                } else {
                    dominationMemberNotes.add(DominationMemberNoteConvert.convert(dominationMemberNoteDO));
                }
            } catch (Exception e) {
                log.error("add error",e);
            }


        }
        return noteMap;
    }

    @Override
    public List<DominationMemberNote> queryByLeaderIdAndSubordinateIds(UserIdCode leaderId, List<UserIdCode> subordinateIds) {
        List<Long> subordinateIdVals = subordinateIds.stream()
                .map(UserIdCode::getValue).collect(Collectors.toList());
        List<DominationMemberNoteDO> dominationMemberNoteDOList =
                dominationMemberNoteMapper.queryByLeaderIdAndSubordinateIds(leaderId.getValue(), subordinateIdVals);
        return dominationMemberNoteDOList.stream()
                .map(DominationMemberNoteConvert::convert).collect(Collectors.toList());
    }

    @Override
    public DominationMemberNote getById(Long id) {
        DominationMemberNoteDO dominationMemberNoteDO = dominationMemberNoteMapper.queryById(id);
        return DominationMemberNoteConvert.convert(dominationMemberNoteDO);
    }

}
